package com.ruoyi.base.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.base.domain.Goods;
import com.ruoyi.base.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.InventoryMapper;
import com.ruoyi.base.domain.Inventory;
import com.ruoyi.base.service.IInventoryService;

/**
 * 库存管理Service业务层处理
 * 
 * @author hp
 * @date 2023-02-04
 */
@Service
public class InventoryServiceImpl implements IInventoryService 
{
    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询库存管理
     * 
     * @param id 库存管理主键
     * @return 库存管理
     */
    @Override
    public Inventory selectInventoryById(Long id)
    {
        return inventoryMapper.selectInventoryById(id);
    }

    /**
     * 查询库存管理列表
     * 
     * @param inventory 库存管理
     * @return 库存管理
     */
    @Override
    public List<Inventory> selectInventoryList(Inventory inventory)
    {
        return inventoryMapper.selectInventoryList(inventory);
    }

    /**
     * 新增库存管理
     * 
     * @param inventory 库存管理
     * @return 结果
     */
    @Override
    public int insertInventory(Inventory inventory)
    {
        return inventoryMapper.insertInventory(inventory);
    }

    /**
     * 修改库存管理
     * 
     * @param inventory 库存管理
     * @return 结果
     */
    @Override
    public int updateInventory(Inventory inventory)
    {
        return inventoryMapper.updateInventory(inventory);
    }

    /**
     * 批量删除库存管理
     * 
     * @param ids 需要删除的库存管理主键
     * @return 结果
     */
    @Override
    public int deleteInventoryByIds(Long[] ids)
    {
        return inventoryMapper.deleteInventoryByIds(ids);
    }

    /**
     * 删除库存管理信息
     * 
     * @param id 库存管理主键
     * @return 结果
     */
    @Override
    public int deleteInventoryById(Long id)
    {
        return inventoryMapper.deleteInventoryById(id);
    }

    @Override
    public void save(Inventory inventory) {
        Goods goods = goodsMapper.selectGoodsById(inventory.getGoodsId());
        Inventory inv = inventoryMapper.selectInventoryByParm(inventory.getWarehouseId(),inventory.getGoodsId());
        if (inv == null) {
            inventory.setColor(goods.getColor());
            inventory.setCode(goods.getCode());
            inventory.setFistInventory("0");
            inventory.setModel(goods.getModel());
            inventory.setName(goods.getName());
            inventory.setSpecification(goods.getSpecification());
            inventory.setStockAmount(inventory.getPrice().multiply(new BigDecimal(Integer.parseInt(inventory.getInventory()))));
            inventory.setUnit(goods.getUnit());
            inventoryMapper.insertInventory(inventory);
        } else {
            if (inventory.getState() == 0) {
                inv.setInventory(String.valueOf(Integer.parseInt(inventory.getInventory()) + Integer.parseInt(inv.getInventory())));
                inv.setStockAmount(inv.getPrice().multiply(new BigDecimal(Integer.parseInt(inv.getInventory()))));
                inventoryMapper.updateInventory(inv);
            } else if (inventory.getState() == 1) {
                inv.setInventory(String.valueOf(Integer.parseInt(inv.getInventory()) -  Integer.parseInt(inventory.getInventory())));
                inv.setStockAmount(inv.getPrice().multiply(new BigDecimal(Integer.parseInt(inv.getInventory()))));
                inventoryMapper.updateInventory(inv);
            }

        }


    }
}
